<template>
  <div class="contain">
    <el-card style="margin: 20px">
      <!-- 表单区域，收集信息 -->
      <el-form label-width="100px">
        <!-- 第一行标签 -->
        <el-row>
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="学科名称" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.subjectName"
                size="small"
                placeholder="请输入学科名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="margin-bottom: 2px">
              <el-button size="small" @click="handelMationClean">
                清空
              </el-button>
              <el-button type="primary" size="small" @click="getSubjectList">
                搜索
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="text-align: right">
            <el-button
              type="success"
              class="right"
              size="small"
              @click="addOpen"
            >
              新增学科
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert type="info" :closable="false">
        <i class="el-icon-info" />共{{ total }}条记录</el-alert
      >
      <!-- 表单区域 -->
      <el-table :data="SubjectList">
        <el-table-column label="序号" prop="id" />
        <el-table-column label="学科名称" prop="subjectName" width="120px" />
        <el-table-column label="创建者" prop="username" width="120px" />
        <el-table-column label="创建日期" prop="addDate" width="220px" />
        <el-table-column
          label="前台是否显示"
          prop="isFrontDisplay"
          width="120px"
        >
          <template #default="{ row }">
            {{ row.isFrontDisplay === 1 ? '显示' : '不显示' }}
          </template>
        </el-table-column>
        <el-table-column
          label="二级目录"
          width="180px"
          prop="twoLevelDirectory"
        >
          <template #default="{ row }">
            <el-switch
              v-model="row.twoLevelDirectory"
              :disabled="true"
              :active-text="'子目录数' + row.twoLevelDirectory"
              inactive-text="无"
              :active-value="1 || 2 || 3 || n"
              :inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column label="标签" prop="tags" />
        <el-table-column label="题目数量" prop="totals" />

        <el-table-column label="操作" width="230" fixed="right">
          <template #default="{ row }">
            <div>
              <!-- 查看详情按钮 -->
              <el-button
                type="text"
                @click="
                  $router.push(
                    `/disci/directory/?state=${row.isFrontDisplay}&name=${row.subjectName}`
                  )
                "
              >
                学科分类
              </el-button>

              <!-- 启用按钮 -->
              <el-button
                type="text"
                @click="
                  $router.push(
                    `/disci/label/?state=${row.isFrontDisplay}&name=${row.subjectName}`
                  )
                "
              >
                学科标签
              </el-button>
              <!-- 修改按钮 -->
              <el-button
                type="text"
                :disabled="row.state === 1"
                @click="resetInterview(row)"
              >
                修改
              </el-button>
              <!-- 删除按钮 -->
              <el-button
                type="text"
                :disabled="row.state === 1"
                @click="handelDelInterview(row)"
              >
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="text-align: right; margin-top: 10px">
        <el-pagination
          :current-page="searFrom.page"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="searFrom.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 修改组件 -->
    <reset-info-com
      :show-dialog="resetDialog"
      :subject-data="subjectData"
      @closeDialog="resetClose"
    />
    <!-- 新增组件 -->
    <add-info-com :show-dialog="addDialog" @closeDialog="addClose" />
  </div>
</template>

<script>
import { changeInterStateAPI } from '@/api/interview'
import { getAllSubjectListAPI, delSubjestAPI } from '@/api/subject'
// 导入组件
import ResetInfoCom from './components/ResetInfoCom.vue'
import AddInfoCom from './components/AddInfoCom.vue'
export default {
  name: 'Interview',
  components: { ResetInfoCom, AddInfoCom },
  data() {
    return {
      addDialog: false,
      resetDialog: false,
      subjectData: {},
      // 详情组件显示
      showDialog: false,
      // 搜索表单对象
      searFrom: {
        page: 1,
        pagesize: 10
        // keyword: '',
        // state: ''
      },
      total: 0,
      editForm: {},
      // 学科列表
      SubjectList: []
    }
  },
  created() {
    this.getSubjectList()
  },
  methods: {
    // 删除学科
    async handelDelInterview(quesData) {
      console.log(quesData)
      await this.$confirm('该操作会永久删除该学科，确认删除吗？', '温馨提示', {
        type: 'warning'
      })
      const resp = await delSubjestAPI(quesData)
      console.log(resp)
      this.$message.success('删除成功')
      this.getSubjectList()
    },
    // 获取学科列表
    async getSubjectList() {
      const { data } = await getAllSubjectListAPI(this.searFrom)
      // console.log('学科列表1', data)
      this.SubjectList = data.items
      this.total = data.counts
    },
    // 打开添加框
    addOpen() {
      this.addDialog = true
    },
    // 关闭添加框
    addClose() {
      this.addDialog = false
      this.getSubjectList()
    },
    // 关闭预览框
    resetClose() {
      this.resetDialog = false
      this.getSubjectList()
    },
    // 修改弹窗打开时
    resetInterview(e) {
      // console.log(e)
      this.subjectData = e
      this.resetDialog = true
    },
    // 预览框打开时
    DialogShow(e) {
      // console.log(e)
      this.subjectData = e
      this.showDialog = true
    },
    // 文章启用/禁用
    async handelChange(e) {
      if (e.state === 1) {
        await this.$confirm('确定禁用吗？', '温馨提示', {
          type: 'warning'
        })
        const resp = await changeInterStateAPI(e.id, 0)
        console.log(resp)
        this.isDisabled = false
        this.$message.success('禁用成功')
        this.getSubjectList()
      } else {
        await this.$confirm('确定启用吗？', '温馨提示', {
          type: 'warning'
        })
        const res = await changeInterStateAPI(e.id, 1)
        console.log(res)
        this.$message.success('启用成功')
        this.getSubjectList()
      }
    },
    // 关闭预览框
    closeDialog() {
      this.showDialog = false
    },
    // 当条数改变时
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searFrom.pagesize = val
      this.getSubjectList()
    },
    // 当页数改变时
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searFrom.page = val
      this.getSubjectList()
    },
    // 清空表单
    handelMationClean() {
      this.searFrom = {}
      this.getSubjectList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}
</style>

